//
//  LPFEmoticonPageView.swift
//  WeiBo
//
//  Created by mac on 16/4/25.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

private let LPFEmoticonPageViewID = "LPFEmoticonPageViewID"

class LPFEmoticonPageView: UICollectionView {

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 设置视图
    private func setupUI(){
        self.backgroundColor = RandomColor()
        
        dataSource = self
        //注册cell
        registerClass(LPFEmoticonPictureViewCell.self, forCellWithReuseIdentifier: LPFEmoticonPageViewID)
        
        // 设置layout
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        
        // 设置itemSize
        layout.itemSize = CGSize(width: SCREENW, height: 216 - 37)
        
        // 设置垂直和水平间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        // 更改我们pageView 的滚动方向
        layout.scrollDirection = .Horizontal
    }
}
// MARK: -  数据源
extension LPFEmoticonPageView: UICollectionViewDataSource{
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return LPFEmoticonTools.sharedTools.allEmoticons.count
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return LPFEmoticonTools.sharedTools.allEmoticons[section].count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(LPFEmoticonPageViewID, forIndexPath: indexPath) as! LPFEmoticonPictureViewCell
        
        cell.emoticons = LPFEmoticonTools.sharedTools.allEmoticons[indexPath.section][indexPath.item]
        
        return cell
    }
}
